// 用户service

import { Service } from "typedi";
import {Prisma} from "@prisma/client"
import prisma from "app/helpers/client";
import { Paging } from "app/utils/Paging";

@Service()
export class UserService {
    // add
    async create(user: Prisma.userCreateInput) {
        // prisma.model--orm(对象--关系型db映射)
        return prisma.user.create({
            data: user
        })
    }

    // del
    async delete(id: number) {
        return prisma.user.delete({
            where: {
                id
            }
        })
    }

    // update
    async update(user: Prisma.userUpdateArgs) {
        return prisma.user.update({
            where: {
                id: user.where.id
            },
            data: user.data
        })
    }

    // query
    async all() {
        const orderFn = (values: string[]) => {
            return values[0];
        }

        const orderField = orderFn(['id']);
        const orderBy = orderFn(['desc']);
        let result = await prisma.user.findMany({
            orderBy: {[orderField]: orderBy}
        })
        return result;
    }

    // 分页  pageNo, pageSize
    async page(userPage: Prisma.userFindManyArgs){
        const get = (values: string[]) => {
            return values[0]
        }
        const orderField = get(['id']);
        const orderby = get(['desc'])
        // 分页数据
        const list = await prisma.user.findMany({
            skip: userPage.skip,   // 跳过多少条
            take: userPage.take,    // 相当于pageSize
            orderBy: {[orderField]: orderby}
        })
        // 总条数
        const rows = await prisma.user.count();
        return new Paging(list, rows);
    }

    // 多表关联
    async queryUserByName(u: string) {
        // userinfo
        // user-role
        // user-permission
        const sql = `select * from user where name='${u}'`;
        const user = await prisma.$queryRaw(Prisma.raw(sql));
        return {user};
    }

    // 根据账号查询
    async queryByAccount(u: Prisma.userWhereUniqueInput) {
        const user = await prisma.user.findUnique({
            where: {
                username: u.username
            }
        })
        // 继续查询用户的角色
        const sql = `select r.* from user u left join role r on u.role=r.id where u.id=${user.id}`
        const role = await prisma.$queryRaw(Prisma.raw(sql));
        return {user, role}
    }

    // 根据视图id查找相应的视图
    async queryViewById(viewid: Prisma.sys_meta_viewWhereUniqueInput) {
        const result = await prisma.sys_meta_view.findUnique({
            where: {
                id: viewid.id
            }
        })
        return result
    }

    // 读取视图的属性列表
    async queryViewPropById(viewId: Prisma.sys_meta_view_propertyWhereInput) {
        const result = await prisma.sys_meta_view_property.findMany({
            where: {
                meta_view_id: viewId.id
            }
        })
        return result;
    }

    // 读取metaData的属性列表
    async queryDataPropById(uid: Prisma.sys_meta_data_propertyWhereUniqueInput) {
        const result = await prisma.sys_meta_data_property.findUnique({
            where: {
                id: uid.id
            }
        })
        return result;
    }

    // 读取metaData
    async queryDataById(uid: Prisma.sys_meta_dataWhereUniqueInput) {
        const result = await prisma.sys_meta_data.findUnique({
            where: {
                id: uid.id
            }
        })
        return result;
    }

    // 动态读取表数据
    async queryAllByMeta(table: string) {
        let result = await prisma[table].findMany();
        return result;
    }

    // 根据引用属性的id与引用属性的表名 查询引用表的对应的数据
    async queryAllByMetaAndId(table: string, id: number) {
        let result = await prisma[table].findUnique({
            where: {
                id
            }
        })
        return result
    }

    // 根据表单id读取表单信息
    async queryFormById(uid: Prisma.sys_meta_formWhereUniqueInput) {
        const result = await prisma.sys_meta_form.findUnique({
            where: {
                id: uid.id
            }
        })
        return result;
    }
}